//
//  AppDelegate.swift
//  GZMarathon
//
//  Created by apple on 2021/10/11.
//

import UIKit
import CoreData
import IQKeyboardManagerSwift
@main
class AppDelegate: UIResponder, UIApplicationDelegate{

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let homeDir = NSHomeDirectory()
        
        print("\(homeDir)")
        loginSence()
        
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        
        initIMFunc()
        
        return true
    }
    
    
    func loginSence(){

        self.window = UIWindow(frame: UIScreen.main.bounds)
        
        let vc = UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "GZLoginController") as! GZLoginController


        let nav = UINavigationController(rootViewController: vc)
        self.window?.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
    }
    
    
    func initIMFunc(){
        
        RCIM.shared().initWithAppKey("8brlm7ufr8l93")
        RCIM.shared().connectionStatusDelegate = self
        RCIM.shared().userInfoDataSource = self
        RCIM.shared().receiveMessageDelegate = self
        RCIM.shared().groupInfoDataSource = self
        
        // 设置全局的属性
        RCIM.shared().globalNavigationBarTintColor = UIColor.white
        UIApplication.shared.applicationIconBadgeNumber = 0
    }

    
    func showMainTabbar(type: String){
        
        let tabVC = UITabBarController()
        tabVC.viewControllers = showWith(type: type)
        tabVC.selectedIndex = 0;
        tabVC.tabBar.tintColor = ThemeColor;
        tabVC.tabBar.isTranslucent = true;
        self.window?.rootViewController = tabVC
        
    }
    //根据角色返回展示的控制器
    private func showWith(type: String) -> [UIViewController] {
        
        var vcs: [UIViewController] = []
        
        if type == "3" {
            //防疫人员
            let home = generateVC(vc: GZAlarmManageController(), img: "tabbar_fangyiguanli_select_off", selImg: "tabbar_fangyiguanli_select_on", title: "防疫管理")
            vcs.append(home)
        }else if type == "2"{
            //急救车驾驶员
            let home = generateVC(vc: GZAEDListController(), img: "tabbar_fangyiguanli_select_off", selImg: "tabbar_fangyiguanli_select_on", title: "AED查询")
            vcs.append(home)
            
        }else{
            //急救人员
            let guide = generateVC(vc: GZGuideListController(), img: "tabbar_jijiuzhidao_select_off", selImg: "tabbar_jijiuzhidao_select_on", title: "急救指导")
            vcs.append(guide)
            
            let home = generateVC(vc: GZAEDListController(), img: "tabbar_fangyiguanli_select_off", selImg: "tabbar_fangyiguanli_select_on", title: "AED查询")
            vcs.append(home)
        }

        
        let chat = generateVC(vc:UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "GZIMListViewController") as! GZIMListViewController, img: "tabbar_phone_select_off", selImg: "tabbar_phone_select_on", title: "视频通话")
        
        
        
        
        
        let mine = generateVC(vc: UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "GZMineController") as! GZMineController, img: "tabbar_wode_select_off", selImg: "tabbar_wode_select_on", title: "我的")
        vcs.append(chat)
        vcs.append(mine)
        return vcs
    }
    
    
    private func generateVC(vc: UIViewController, img: String, selImg: String, title: String) -> EBBaseNavController{
           
           vc.tabBarItem.title = title
           vc.tabBarItem.image = UIImage(named: img)?.withRenderingMode(.alwaysOriginal)
           vc.tabBarItem.selectedImage = UIImage(named: selImg)?.withRenderingMode(.alwaysOriginal)
           return EBBaseNavController(rootViewController: vc)
    }
    

}


extension AppDelegate:RCIMConnectionStatusDelegate , RCIMUserInfoDataSource ,RCIMReceiveMessageDelegate,RCIMGroupInfoDataSource{
    //其他设备登录时调用此方法
    func onRCIMConnectionStatusChanged(_ status: RCConnectionStatus) {
            print("%ld",status)
            if status == RCConnectionStatus.ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT{
                
                
                print("下线通知,该用户在其他设备上登录，你已被迫下线")

            }
        }
        /*!
         *获取用户信息
         */
        func getUserInfo(withUserId userId: String!, completion: ((RCUserInfo?) -> Void)!) {
            if userId == nil{
                return
            }
            let userinfo = RCUserInfo.init(userId: "text1", name: "TTT", portrait: "http://localhost/简单HTML/citi标签.html")
            completion(userinfo)
        }
        /*!
         *接收消息的回调方法
         */
        func onRCIMReceive(_ message: RCMessage!, left: Int32) {
            
            print("%@",message)
        }
        
        /**
         *  获取群组信息
         */
        func getGroupInfo(withGroupId groupId: String!, completion: ((RCGroup?) -> Void)!) {
            if groupId == nil {
                return
            }
        }
    
        func logoutRongCloud() {
            RCIM.shared().logout()
        }
}
